from pyecharts.charts import Map
from pyecharts.faker import Faker
import pyecharts.options as opts
import json

f = open('/Users/yanfeixingsan/Documents/进阶之路/python/test/可视化案例数据/地图数据/疫情.txt', 'r', encoding='UTF-8')
data_json = f.read()
f.close()

dict_map = {
    '北京': '北京市',
    '上海': '上海市',
    '重庆': '重庆市',
    '深圳': '深圳市',
    '内蒙古': '内蒙古自治区',
    '西藏': '西藏自治区',
    '新疆': '新疆维吾尔自治区',
    '宁夏': '宁夏回族自治区',
    '广西': '广西壮族自治区',
    '香港': '香港特别行政区',
    '澳门': '澳门特别行政区',
}

# 转换
data_dict = json.loads(data_json)
province_data_list = data_dict['areaTree'][0]['children']
data_list = []
for province_data in province_data_list:
    province_name = ''
    # 如果没有异常说明需要字典映射， 有异常说明不需要
    try:
        if dict_map[province_data['name']]:
            province_name = dict_map[province_data['name']]
    except Exception as e:
        province_name = province_data['name'] + "省"

    province_confirm = province_data['total']['confirm']
    data_list.append([province_name, province_confirm])

print(data_list)

c = (
    Map()
    .add("各省份确诊人数", data_list, "china")
    .set_global_opts(
        title_opts=opts.TitleOpts(title="全国疫情地图"), visualmap_opts=opts.VisualMapOpts(
            is_show=True,
            is_piecewise=True,
            pieces=[
                {"min": 1, "max": 99, 'label': "1~99人", "color": "#CCFFFF"},
                {"min": 100, "max": 999, "label": "100~999人", "color": "#FFFF99"},
                {"min": 1000, "max": 4999, "label": "1000~4999人", "color": "#FF9966"},
                {"min": 5000, "max": 9999, "label": "5000~9999人", "color": "#FF6666"},
                {"min": 10000, "max": 99999, "label": "10000~99999人", "color": "#CC3333"},
                {"min": 100000, "label": "100000+", "color": "#990033"}
            ]
        )
    )
    .render('全国疫情确诊地图.html')
)
